package cn.edu.scau.cmi.front.controller;

import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.util.CmiSetUtil;
import cn.edu.scau.cmi.ema.domain.*;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;

@Controller("FrontUserController")
public class FrontUserController extends FrontCmiEmaController {
	
	@RequestMapping(value = "/registerPage", method =RequestMethod.GET)
	public ModelAndView registert(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("register/register.jsp");
		return mav;
		
	}
	
	@RequestMapping(value = "/register", method =RequestMethod.POST)
	public ModelAndView register(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String username  = request.getParameter("username");
		String password  = request.getParameter("password");
		
//		用户名不能重复，如果存在，就返回
		Set<User> existingUser = userDAO.findUserByUsername(username);
		if(existingUser.size()>0) {
			mav.addObject("user", existingUser);
			mav.addObject("message", "用户名已经存在，请重新输入用户名");

			mav.setViewName("register/register.jsp");
			return mav;
		}
		
		User user = new User();
		user.setUsername(username);
		user.setName(username);
		user.setPassword(password);
		userService.saveUser(user);
		
		mav.addObject("user", user);

		mav.setViewName("front/frontEditUser.jsp");

		return mav;
	}

//	前端用户登录
	@RequestMapping(value = "/login", method =RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String username  = request.getParameter("username");
		String password  = request.getParameter("password");
		
		User user = CmiSetUtil.getSoleInstance(userDAO.findUserByUsername(username));
		if(user.getPassword().equals(password)) {
			mav.setViewName("front/frontPbulic.jsp");//首页
		}else {
			mav.setViewName("user/editUser.jsp");
		}
		return mav;
	}
	

	@RequestMapping("/frontViewUser")
	public ModelAndView viewUser(@RequestParam Integer userId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("user", userDAO.findUserByPrimaryKey(userId));
		mav.setViewName("user/viewUser.jsp");
		return mav;
	}


	@RequestMapping("/frontEditUser")
	public ModelAndView editUser(@RequestParam Integer userId) {

		ModelAndView mav = new ModelAndView();

		User user = userDAO.findUserByPrimaryKey(userId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("user", user);

		// 把所有的引用user对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(userService.loadUserObjectReferencedSet(user));

		// 将编辑页面user添加到mav中
		mav.setViewName("user/editUser.jsp");
		return mav;
	}

	@RequestMapping("/frontNewUser")
	public ModelAndView newUser() {
		ModelAndView mav = new ModelAndView();
		User user = new User();

		mav.addObject("user", user);

		// 把所有的引用user对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(userService.loadUserObjectReferencedSet(user));

		mav.addObject("newFlag", true);
		mav.setViewName("user/editUser.jsp");

		return mav;
	}
	

	@RequestMapping("/frontSaveUser")
	public ModelAndView saveUser(@ModelAttribute User user) {

		ModelAndView mav = new ModelAndView();

		User existingUser = CmiHibernateProxyUtil
				.deproxy(userDAO.findUserByPhysicalPrimaryKey(user.getName(), user.getBirthday(), user.getEmail()));

		// 新建记录
		if (user.getId() == null) {
			if (user.equals(existingUser)) {

				mav.addAllObjects(userService.loadUserClassReferencedSet(user));
				// mav = editContinue(mav, user);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("user/editUser.jsp");
			} else {
				userService.saveUser(user);
				mav.setViewName("forward:/listUsers");
			}
		}
		// 编辑记录
		else {
			User originUser = CmiHibernateProxyUtil.deproxy(userDAO.findUserByPrimaryKey(user.getId()));
			if (!user.equals(originUser) && user.equals(existingUser)) {
				mav.addAllObjects(userService.loadUserClassReferencedSet(user));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("user/editUser.jsp");
			} else {
				userService.saveUser(user);
				mav.setViewName("forward:/listUsers");
			}
		}
		return mav;
	}
}